package com.hospital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.dao.RegisterDao;
import com.hospital.entity.Register;
import com.hospital.service.RegisterService;
import com.hospital.vo.RegisterVO;
import com.hospital.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


/**
 * @author coder_zzm
 * @date 2022/3/24 16:13
 */
@Slf4j
@DubboService
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RegisterDao registerDao;

    /**
     * 查询所有挂号信息
     *
     * @param pageNum  页码
     * @param pageSize 数量
     * @return 返回所有的结果集
     */
    @Override
    public ResponseVO getAll(int pageNum, int pageSize) {
        log.info("开始获取所有挂号信息");
        List<Register> list = registerDao.getAll(pageNum, pageSize);
        list.forEach(System.out::println);
        return ResponseVO.buildSuccessInstance(list);
    }

    /**
     * 根据条件查询挂号信息
     *
     * @param rid          编号
     * @param registerName 姓名
     * @param department   科室
     * @return 挂号的详细信息
     */
    @Override
    public ResponseVO findRegister(String rid, String registerName, String department) {
        log.info("开始根据条件查询挂号的详细信息");
        Register register = registerDao.findRegister(rid, registerName, department);
        log.info(String.valueOf(register));
        return ResponseVO.buildSuccessInstance(register);
    }

    /**
     * 添加信息
     *
     * @param registerVO 统一 vo
     * @return 单条记录
     */
    @Override
    public ResponseVO addRegister(RegisterVO registerVO) {
        log.info("开始添加挂号信息");
        Register register = new Register();
        BeanUtil.copyProperties(registerVO, register);
        registerDao.addRegister(register);
        return ResponseVO.buildSuccessInstance(ResponseVO.RESP_SUCCESS);
    }

    /**
     * 批量删除
     *
     * @param rids 编号
     * @return 是否删除成功和受影响行数
     */
    @Override
    public ResponseVO batchDelete(String[] rids) {
        for (String rid : rids) {
            System.out.println("rid = " + rid);
        }
        log.info("开始删除药品信息");

        // 返回受影响行数
        Integer row = registerDao.batchDelete(rids);
        if (row > 0) {
            return ResponseVO.buildSuccessInstance(ResponseVO.RESP_SUCCESS, row);
        } else {
            return ResponseVO.buildSuccessInstance(ResponseVO.DELETE_FAIL, 0);
        }
    }

    /**
     * 根据编号查询单条信息
     *
     * @param rid 编号
     * @return 单条记录
     */
    @Override
    public ResponseVO getRegisterById(String rid) {
        log.debug("开始根据编号查询挂号的详细信息");
        Register registerById = registerDao.getRegisterById(rid);
        log.debug("查询到的信息：" + registerById);
        return ResponseVO.buildSuccessInstance(registerById);
    }

    /**
     * 修改信息
     *
     * @param registerVO 统一 vo
     * @return 单条记录
     */
    @Override
    public ResponseVO updateRegister(RegisterVO registerVO) {
        log.info("开始修改挂号信息");
        Register register = new Register();
        BeanUtils.copyProperties(registerVO, register);
        registerDao.updateRegister(register);
        return ResponseVO.buildSuccessInstance(ResponseVO.RESP_SUCCESS);
    }
}
